package com.tboss.template.system.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import com.tboss.global.utils.useful.SecurityUtils;
import com.tboss.template.common.dto.NavigationDto;
import com.tboss.template.common.springSecurity.SecurityBuildService;
import com.tboss.template.common.utils.DateUtils;
import com.tboss.template.system.dao.CodeDao;
import com.tboss.template.system.dao.CodeTypeDao;
import com.tboss.template.system.dao.FunctionDao;
import com.tboss.template.system.dto.CodeTypeDto;
import com.tboss.template.system.dto.FunctionDto;
import com.tboss.template.system.dto.GroupDto;
import com.tboss.template.system.dto.PrivateGrpFnDto;

@Service
public class FunctionService {
	
	@Autowired SystemService systemService;
	@Autowired FunctionDao fnDao;
	@Autowired CodeTypeDao codeTypeDao;
	@Autowired CodeDao codeDao;
	
	private List<CodeTypeDto> getCodeTypeList(){
		CodeTypeDto codeTypeDto = new CodeTypeDto();
		codeTypeDto.setPagingYn("N");
		
		return this.codeTypeDao.getCodeTypeList(codeTypeDto);
	}
	
	private Integer getFnListCnt(FunctionDto dto){
		return this.fnDao.getFnListCnt(dto);
	}
	
	private List<FunctionDto> getFnList(FunctionDto dto){
		return this.fnDao.getFnList(dto);
	}
	
	public void setModelFnList(Integer reqPage, Model model, FunctionDto dto){
		if(reqPage != null) dto.setCurrentPage(reqPage);

		Integer rowsCnt = this.getFnListCnt(dto);
		NavigationDto pageNavi = new NavigationDto(rowsCnt, dto.getRowPerPage(), dto.getCurrentPage());
		
		model.addAttribute("sysBase", this.systemService.getSysBase(dto));
		model.addAttribute("enCodeType", dto.getSysCodeType());
		model.addAttribute("enCodeTypeStatus", dto.getSysCodeTypeStatus());
		model.addAttribute("pageNavi", pageNavi);
		model.addAttribute("fnList", this.getFnList(dto));
	}
	
	public void fnRegForm(Model model, FunctionDto dto){
		int roleWeight = SecurityBuildService.currUsrDto.getRoleWeight();
		
		model.addAttribute("enCodeType", dto.getSysCodeType());
		model.addAttribute("enCodeTypeStatus", dto.getSysCodeTypeStatus());
		model.addAttribute("codeTypes", this.getCodeTypeList());
		
		PrivateGrpFnDto pDto = new PrivateGrpFnDto();
		pDto.setRoleWeight(roleWeight);
		model.addAttribute("privateGrpLst", this.fnDao.getPrivateGrpLst(pDto));
	}
	
	public void fnRegForm(Model model, String fnId, FunctionDto dto){
		int roleWeight = SecurityBuildService.currUsrDto.getRoleWeight();
		
		model.addAttribute("enCodeType", dto.getSysCodeType());
		model.addAttribute("enCodeTypeStatus", dto.getSysCodeTypeStatus());
		model.addAttribute("codeTypes", this.getCodeTypeList());
		model.addAttribute("fnInfo", this.fnDao.getFnInfo(fnId));
		
		PrivateGrpFnDto pDto = new PrivateGrpFnDto();
		pDto.setRoleWeight(roleWeight);
		pDto.setCdId(fnId);
		model.addAttribute("privateGrpLst", this.fnDao.getPrivateGrpLst(pDto));
	}
	
	public void fnGroups(Model model, String fnId){
		model.addAttribute("grpList", this.fnDao.getFnGroups(fnId));
	}
	
	public void addFnInfo(FunctionDto dto, PrivateGrpFnDto items){
		dto.setFnRegId(SecurityUtils.getCurrentUserName());
		dto.setFnRegDate(DateUtils.getDateFormat("yyyyMMddHHmmss"));
		this.fnDao.addFnInfo(dto);
		this.privateGrpProc("new", items, dto);
	}
	
	public void updateFnInfo(FunctionDto dto, PrivateGrpFnDto items){
		dto.setFnModId(SecurityUtils.getCurrentUserName());
		dto.setFnModDate(DateUtils.getDateFormat("yyyyMMddHHmmss"));
		this.fnDao.updateFnInfo(dto);
		
		this.privateGrpProc("revise", items, dto);
		
		//Change Columns: cdId, fnParentId, fnGroupId
		if(!dto.getCdId().equals(dto.getFnId())){
			this.fnDao.updateFnGroups(dto);
		}
		
		List<GroupDto> fnGrpList = this.fnDao.getFnGroups(dto.getFnId());
		if(fnGrpList != null && fnGrpList.size() > 0){
			ArrayList<String> grpList = new ArrayList<String>();
			for(GroupDto grp : fnGrpList){
				grpList.add(grp.getGrpId());
			}
			this.fnDao.updateGrpChgChkField(grpList);
		}
	}
	
	private void privateGrpProc(String type, PrivateGrpFnDto items, FunctionDto dto){
		if(type != null && type.equals("revise")){
			this.fnDao.deletePrivateGrpLst(dto.getCdId());
		} 
		
		List<PrivateGrpFnDto> list = items.getItems();		
		if(list != null && list.size() > 0){
			for(PrivateGrpFnDto d : list){
				d.setCdId(dto.getFnId());
			}
			
			this.fnDao.addPrivateGrpLst(items);
		}
	}
}
